package sair.player.acts;

import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JList;

import sair.sacoms.Urler;

public class PlayerList {
	private ArrayList<String> listPath = new ArrayList<String>();
	private DefaultListModel<String> listHead = new DefaultListModel<String>();
	DefaultListModel<String> listName = new DefaultListModel<String>();

	public PlayerList(JList<String> list,JList<String> listh) {
		list.setModel(listName);
		listh.setModel(listHead);
	}

	public int listSize() {
		int pathSize = listPath.size();
		int nameSize = listName.size();

		if (pathSize > nameSize)
			return nameSize;

		return pathSize;
	}

	void addPath(String path) {
		Urler file = new Urler(path);
		listPath.add(path);
		listName.addElement(file.getFileName());
		listHead.addElement(String.valueOf(listHead.getSize()));
	}

	void clearAll() {
		listName.clear();
		listPath.clear();
		listHead.clear();
	}

	public ListPage get(int index) {
		if (index < 0 || index >= listPath.size() || index >= listName.size())
			return null;
		return new ListPage() {

			@Override
			public String getPath() {
				return listPath.get(index);
			}

			@Override
			public String getName() {
				return listName.get(index);
			}

		};
	}

	ListPage remove(int index) {
		if (index < listSize() && index >= 0) {
			String path = listPath.remove(index);
			String name = listName.remove(index);
			if (path != null && name != null) {
				listHead.remove(listHead.size() - 1);
				return new ListPage() {

					@Override
					public String getPath() {
						return path;
					}

					@Override
					public String getName() {
						return name;
					}

				};
			}
		}
		return null;
	}
}
